package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.CarModelSelectBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class X3CarModelHorizotalAdapter extends RecyclerView.Adapter<X3CarModelHorizotalAdapter.CarModelHorizotalHolder> {

    private List<CarModelSelectBean> mList;

    private Context mContext;
    private List<CarModelSelectBean> selectSingleList;
    private String brandName;
    private String brandValue;

    public X3CarModelHorizotalAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();


    }
    public void setDataList(List<CarModelSelectBean> mList,List<CarModelSelectBean> selectSingleList,String brandName,String brandValue) {
        this.mList = mList;
        this.selectSingleList = selectSingleList;
        this.brandName = brandName;
        this.brandValue = brandValue;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public X3CarModelHorizotalAdapter.CarModelHorizotalHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new X3CarModelHorizotalAdapter.CarModelHorizotalHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_crm_filter_car_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3CarModelHorizotalAdapter.CarModelHorizotalHolder holder, int position) {
        if (mList.get(position).brandName.contains("<em>")){
            String tmp = mList.get(position).brandName;
            tmp = tmp.substring(tmp.indexOf(">")+1,tmp.indexOf("</"));
            mList.get(position).brandName = tmp;
        }
            holder.tv_name.setText(mList.get(position).brandName+" "+mList.get(position).modelName);
            holder.iv_img.setOnClickListener(v -> {
                //全选ALL清空    非全选一项一项的移除        selectsingle
                    if(mList.get(position).brandValue.equals(brandValue) && mList.get(position).modelValue.equals("ALL")){//全选了
                        selectSingleList.clear();
                    }else {
                        Iterator<CarModelSelectBean> iterator = selectSingleList.iterator();
                        while (iterator.hasNext()) {
                            CarModelSelectBean next = iterator.next();
                            if (next == null) {
                                continue;
                            }
                            if (X3StringUtils.isEmpty(next.modelValue)) {
                                continue;
                            }
                            if (next.brandValue.equals(mList.get(position).brandValue) && next.modelValue.equals(mList.get(position).modelValue)) {
                                iterator.remove();
                            }
                        }
                    }
                mList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position,mList.size() - position);
                if(carModleHorDeleteListener!=null){
                    carModleHorDeleteListener.deleteHorModel();
                }

            });

    }

    CarModleHorDeleteListener carModleHorDeleteListener;

    public void setiOnDeleteHor(CarModleHorDeleteListener carModleHorDeleteListener) {
        this.carModleHorDeleteListener = carModleHorDeleteListener;
    }

    public interface CarModleHorDeleteListener {
        void deleteHorModel();
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class CarModelHorizotalHolder extends RecyclerView.ViewHolder {
        private TextView tv_name;
        private ImageView iv_img;
        public CarModelHorizotalHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_img = itemView.findViewById(R.id.iv_img);

        }
    }
}
